package com.ray.friendslist.service;

import java.util.List;

import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Intent;
import android.util.Log;

import com.ray.friendslist.content.ContentManager;
import com.ray.friendslist.content.contract.FriendsContract;
import com.ray.friendslist.content.task.ParamCallback;
import com.ray.friendslist.service.common.CommonService;
import com.ray.friendslist.token.VkTokenManager;

public class VKFriendsService extends CommonService {

	@Override
	protected void task(Intent intent) {
		Log.i("ray", "FRIENDS SERVICE STARTED");
		ContentManager.getInstance().getVkFriends(
				new ParamCallback<List<JSONObject>>() {

					public void onSuccess(final List<JSONObject> c) {
						Intent intent = new Intent(ACTION_ON_SUCCESS);
						sendBroadcast(intent);
						new Thread(new Runnable() {

							public void run() {
								// TODO Auto-generated method stub
								for (JSONObject j : c) {
									Log.d("ray", j.toString());
									ContentValues contentValues = new ContentValues();
									contentValues.put("data", j.toString());
									getContentResolver()
											.insert(FriendsContract.FriendsColumns.CONTENT_URI,
													contentValues);
								}
							}
						}).start();
					}

					public void onError(Throwable e) {
						// TODO Auto-generated method stub
						Log.d("ray", "ONERROR");
						Intent intent = new Intent(ACTION_ON_ERROR);
						intent.putExtra(EXTRA_KEY_MESSAGE, e.getMessage());
					}
				},
				"https://api.vk.com/method/friends.get?fields=first_name,photo_medium,uid,nickname,bdate,photo_big,city&order=hints&access_token="
						+ VkTokenManager.getTooken());
	}

}
